#***********************************************************
#* Copyright (c) 2006, Zhimin Zhan.
#* Distributed open-source, see full license in MIT-LICENSE
#***********************************************************

require 'test/unit'

module RWebUnit

class WebTestCase < Test::Unit::TestCase
  include RWebUnit::Utils

  attr_reader :web_tester

  def initialize(name=nil)
    super(name) if name
    @web_tester = WebTester.new
  end

  def default_test
    super unless(self.class == WebTestCase)
  end

  def getTestContext
    @web_tester.test_context
  end

  def beginAt(url)
    @web_tester.beginAt(url)
  end

  def openBrowser(baseUrl, relativeUrl)
    getTestContext().base_url = baseUrl
    beginAt(relativeUrl)
  end

  def closeBrowser
    @web_tester.closeBrowser
  end

  def goBack()
    @web_tester.goBack
  end

  def goForward()
    @web_tester.goForward
  end

  def gotoPage(page)
    @web_tester.gotoPage(page)
  end

  # assertions
  def assertTitleEquals(title)
    @web_tester.assertTitleEquals(title)
  end

  def assertTextPresent(text)
    @web_tester.assertTextPresent(text)
  end

  def assertTextNotPresent(text)
    @web_tester.assertTextNotPresent(text)
  end

  # textfields
  def setFormElement(elementName, elementValue)
    @web_tester.setFormElement(elementName, elementValue)
  end

  def assertTextPresentInTextField(textfieldName, text, msg = nil)
    @web_tester.assertTextPresentInTextField(textfieldName, text, msg)
  end

  #links
  def clickLinkWithText(linkText)
    @web_tester.clickLinkWithText(linkText)
  end

  def assertLinkPresentWithText(linkText)
    @web_tester.assertLinkPresentWithText(linkText)
  end

  def assertLinkNotPresentWithText(linkText)
    @web_tester.assertLinkNotPresentWithText(linkText)
  end

  ##
  # buttons

  # submit the form using the first (index) submit button
  def submit()
    @web_tester.submit()
  end

  def submit(buttonId)
    @web_tester.submit(buttonId)
  end

  def clickButton(buttonId)
    @web_tester.clickButton(buttonId)
  end

  def clickButtonWithCaption(caption)
    @web_tester.clickButtonWithCaption(caption)
  end

  def clickButtonWithValue(value)
    @web_tester.clickButtonWithValue(value)
  end

  def assertButtonNotPresent(buttonID)
    @web_tester.assertButtonNotPresent(buttonID)
  end

  def assertButtonNotPresentWithText(text) 
    @web_tester.assertButtonNotPresentWithText(text)
  end
 
  def assertButtonPresent(buttonID) 
    @web_tester.assertButtonPresent(buttonID)
  end

  def assertButtonPresentWithText(buttonID) 
    @web_tester.assertButtonPresentWithText(buttonID)
  end

  # checkbox
  def checkCheckbox(checkBoxName)
    @web_tester.checkCheckbox(checkBoxName)
  end

  def uncheckCheckbox(checkBoxName)
    @web_tester.uncheckCheckbox(checkBoxName)
  end

  # combo box
  def selectOption(selectName, option)
    @web_tester.selectOption(selectName, option)
  end

  # dhtml related
  def assertElementPresent(elementID)
    @web_tester.assertElementPresent(elementID)
  end

  def assertElementNotPresent(elementID)
    @web_tester.assertElementNotPresent(elementID)
  end

  # ---
  # For deubgging
  # ---
  def dumpResponse(stream = nil)
    @web_tester.dumpResponse(stream)
  end

end

end